Component({
  options: {
    addGlobalClass: true,
  },
  data: {
  	cart:{},
  	cartlength:0,
  	total:0,
  	alls:0 //是否全选
  },
  attached() {
  	var that=this.data;
  	var len=0;
  	var all=1;
  	var total=0
  	
  	that.cart=wx.getStorageSync('cart');
  	
  	for(var i in that.cart){
  		len++;
  		if(!that.cart[i].selects && all!=0){
	  			all=0;
  		}
  		if(that.cart[i].selects){
  			total=Number(total) + (Number(that.cart[i].price) * Number(that.cart[i].num));
  			
  		}
  	}
  	this.setData({
  		cart:that.cart,
  		total:parseFloat(total).toFixed(2),
  		alls:all,
      cartlength: len
    })
  	console.log(len)
	},
  methods: {
	  selects:function(e) {//选择按钮
	  	var that=this,
	  			cur=that.data.cart[e.currentTarget.dataset.i];
	  			console.log(cur.selects)
	  	if(cur.selects){
	  		delete cur.selects 
	  	}else{
	  		cur.selects=1;
	  	}
	  	console.log(cur)
	  	that.setlist()
	  },
	  selectsAll:function(e) {//全选
	  	var that=this;
	  	var all=that.data.alls?0:1;
	  	for(var i in that.data.cart){
	  		console.log(that.data.cart[i])
	  		that.data.cart[i]["selects"]=all;
	  	}
	  	that.setlist()
	  },
	  add:function(e){//增加数量
	  	var that=this,
	  			cur=that.data.cart[e.currentTarget.dataset.i]
	  	var c=cur.num;
	  			c++;
	  			cur.num=c;
	  	that.setlist()
	  },
	  lower:function(e){//减少数量
	  	var that=this,
	  			cur=that.data.cart[e.currentTarget.dataset.i]
	  	var c=cur.num;
	  			c--;
	  			if(c<=0){
	  				wx.showModal({
						  title: '删除',
						  content: '确定要删除当前商品吗？',
			            success(res) {
		                if (res.confirm) {
		                	delete that.data.cart[e.currentTarget.dataset.i]
	  									that.setlist()
		                }else if(res.cancel) {
		              	}
			           }
			       })
	  				return false;
	  			}
	  			cur.num=c;
	  			that.setlist()
	  },
	  setlist:function(){//更新缓存
	  	var that=this
	  	var cart=this.data.cart;
	  	var all=1;
	  	var total=0;
	  	for(var i in that.data.cart){
	  		if(!that.data.cart[i].selects && all!=0){
	  			all=0;
	  		}
	  		if(that.data.cart[i].selects){
	  			total=Number(total)+(Number(that.data.cart[i].price)*Number(that.data.cart[i].num));
	  		}
	  	};
	  	
	  	this.setData({
	  		total:parseFloat(total).toFixed(2),
	  		alls:all,
	      cart: cart
	    })
	  	wx.setStorage({
			   key: "cart",
			   data: cart //需要存储的数据
			})
	  },cartset:function(){
	  	this.triggerEvent('setcart')
	  },
	  gopay:function(e){
	  	var that=this;
  		var orderGoodsList=[];
  		var total=0;
  		for(var i in that.data.cart){
	  		if(that.data.cart[i].selects){
	  			orderGoodsList.push(that.data.cart[i])
	  			total=Number(total)+(Number(that.data.cart[i].price)*Number(that.data.cart[i].num));
	  		}
	  	};
	  	
	  	if(!orderGoodsList.length){
	  		wx.showToast({
	        title: "请勾选购物车商品！",
	        icon: "none"});
	  		return false;
	  	}
	  	
  		var orderdata={
			  orderGoodsList: orderGoodsList,
			  total:total,//合计
			  remark: "",//备注
			  storeId: 0,//供应商ID
			  totalAmount: 0,//总金额
			}
  		wx.setStorage({
			   key: "orderlist",
			   data: orderdata //需要存储的数据
			})
	  	wx.navigateTo({
			  url: '/pages/cart/pay/pay?type=cart'
			})
	  }
  }
})
